package com.itboye.shop.category.store;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.itboye.shop.IndexFragment;
import com.itboye.shop.R;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.bean.GoodsDetailBean;
import com.itboye.shop.common.CarouselView;
import com.itboye.shop.common.goodsdetail.McoyProductContentPage;
import com.itboye.shop.common.goodsdetail.McoyProductDetailInfoPage;
import com.itboye.shop.common.goodsdetail.McoySnapPageLayout;
import com.itboye.shop.common.goodsdetail.PagerSlidingTabStrip;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.ShopPresenter;
import com.itboye.shop.presenter.UserPresenter;
import com.itboye.shop.util.helper.ImageLoader;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.MAlert;
import com.itboye.shop.util.loadutil.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;


/**
 * 商品详情
 */
public class GoodDetailActivity extends BaseActivity implements Observer {
    ImageView back;
    TextView tvTitle;
    TextView goods_detail_title;//标题
    TextView goods_detail_price;//价格
    TextView goods_detail_jianjie;//简介
    TextView goods_detail_youfei;//邮费
    TextView goods_guige;//规格
    TextView goods_detail_gouwuche, goods_detail_jiarugouwuche;
    LinearLayout li_choose_guige;//规格
    TextView goods_detail_price02;//原件
    CarouselView topLunbo;
    GoodsDetailBean detailBean = new GoodsDetailBean();
    ShopPresenter shopPresenter;
    UserPresenter userPresenter;
    RelativeLayout layout;
    LinearLayout shuxing;//属性
    String goodsId;
    String goodsName;
    PopupWindow mPopWin;
    TextView detail;
    Button addShopcart, lijibuy;
    int dian = 0;
    TextView tv_num;
    String type;//来标记是选择类型 还是加入购物车
    StringBuffer descid;
    StringBuffer moreCount;
    String uid;
    ImageView img_other;
    String tempSkuId = null;
    ViewPager pager;
    private McoySnapPageLayout flipLayout;
    private McoyProductContentPage bottomPage = null;
    private McoyProductDetailInfoPage topPage = null;
    LinearLayout re_collection;
    RelativeLayout store, kefuLine;
    String[] titles = {"图文详情", "商品评价", "商品咨询"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goodsdetail);
        goodsId = getIntent().getStringExtra("goodsId");
        shopPresenter = new ShopPresenter(this);
//        userPresenter = new UserPresenter(this);
//        showProgressDialog("数据加载中,请稍后...", true);

//        img_other.setBackgroundResource(R.drawable.gouwuche);
//        img_other.setVisibility(View.VISIBLE);
        goodsName = getIntent().getStringExtra("goodsName");
//        tvTitle.setText(goodsName);
        uid = (String) SPUtils.get(this, null, Const.ID, "");
        topPage = new McoyProductDetailInfoPage(this, getLayoutInflater()
                .inflate(R.layout.mcoy_produt_detail_layout, null));
        bottomPage = new McoyProductContentPage(this, getLayoutInflater()
                .inflate(R.layout.mcoy_product_content_page, null));
        flipLayout.setSnapPages(topPage, bottomPage);
        initView();

        pager.setAdapter(new MyAdapter(getSupportFragmentManager(), titles));
        ((PagerSlidingTabStrip) flipLayout.findViewById(R.id.tabs))
                .setViewPager(pager);

//        detailgridview = (GridView) flipLayout
//                .findViewById(detailgridview);

        shopPresenter.mallXiangQing(goodsId);
    }

    TextView txt_goods_name, txt_share, txt_relprice, txt_tuwenxiangqing, txt_old_goodsprice, txt_kuaidi, txt_xiaoliang, txt_chandi, txt_tips;

    private void initView() {
        txt_goods_name = (TextView) flipLayout.findViewById(R.id.txt_goods_name);
        txt_share = (TextView) flipLayout.findViewById(R.id.txt_share);
        txt_relprice = (TextView) flipLayout.findViewById(R.id.txt_relprice);
        txt_tuwenxiangqing = (TextView) flipLayout.findViewById(R.id.txt_tuwenxiangqing);
        txt_old_goodsprice = (TextView) flipLayout.findViewById(R.id.txt_old_goodsprice);
        txt_kuaidi = (TextView) flipLayout.findViewById(R.id.txt_kuaidi);
        txt_xiaoliang = (TextView) flipLayout.findViewById(R.id.txt_xiaoliang);
        txt_chandi = (TextView) flipLayout.findViewById(R.id.txt_chandi);
        txt_tips = (TextView) flipLayout.findViewById(R.id.txt_tips);
        pager = (ViewPager) flipLayout.findViewById(R.id.pager);
        topLunbo = (CarouselView) flipLayout.findViewById(R.id.topLunbo);
    }

    public class MyAdapter extends FragmentStatePagerAdapter {
        String[] _titles;

        public MyAdapter(FragmentManager fm, String[] titles) {
            super(fm);
            _titles = titles;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return _titles[position];
        }

        @Override
        public int getCount() {
            return _titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment bfrag2 = null;
            if (position == 0) {
                bfrag2 = new IndexFragment();
            } else if (position == 1) {
                bfrag2 = new IndexFragment();
            } else if (position == 2) {
                bfrag2 = new IndexFragment();
            }
            return bfrag2;
        }
    }

    JiaRuGouWuCheDialog dialog;

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.kefuLine:
                break;
            case R.id.addShopcart:
                if (detailBean!=null) {
                    if (dialog == null)
                        dialog = new JiaRuGouWuCheDialog(detailBean, false);
                    dialog.show(getSupportFragmentManager(), "");
                }

                break;
            case R.id.lijibuy:
                if (detailBean!=null) {
                    if (dialog == null)
                        dialog = new JiaRuGouWuCheDialog(detailBean, true);
                    dialog.show(getSupportFragmentManager(), "");
                }
                break;
            case R.id.re_collection:
                break;
            case R.id.store:
                break;
//            case R.id.goods_detail_jiarugouwuche:
//                type = "jiaru";
//                if (dialog == null)
//                    dialog = new JiaRuGouWuCheDialog(detailBean, false);
//                dialog.show(getSupportFragmentManager(), "");
//                break;
//            case R.id.img_other:
//                intent = new Intent(this, ShopCartActivity.class);
//                startActivity(intent);
//                break;
//            case R.id.goods_guige:
//                type = "leixing";
//                if (dialog == null)
//                    dialog = new JiaRuGouWuCheDialog(detailBean, false);
//                dialog.show(getSupportFragmentManager(), "");
//                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == ShopPresenter.mall_xq_success) {
                detailBean = (GoodsDetailBean) entity.getData();
                xiqngqing(detailBean);
            } else if (entity.getEventType() == ShopPresenter.mall_xq_fail) {
                MAlert.alert(entity.getData() + "");
            } else if (entity.getEventType() == UserPresenter.addAll_success) {
//                ByAlert.alert(entity.getData() + "");
//                Intent intent = new Intent(this, ShopCartActivity.class);
//                startActivity(intent);
            } else if (entity.getEventType() == UserPresenter.addAll_fail) {
                MAlert.alert(entity.getData() + "");
            }


        }
        try {
            closeProgressDialog();
        } catch (Exception e) {

        }
    }

    /**
     * 商品详情
     */
    private void xiqngqing(GoodsDetailBean detailBean) {
        txt_goods_name.setText(detailBean.getName());
        if (!detailBean.getSku_list().get(0).getPrice().equals("")) {
            txt_relprice.setText("￥" + Double.parseDouble(detailBean.getSku_list().get(0).getPrice()) / 100);
        } else {
            txt_relprice.setText("￥0");
        }
        if (!detailBean.getSku_list().get(0).getOri_price().equals("")) {
            txt_old_goodsprice.setText("￥" + Double.parseDouble(detailBean.getSku_list().get(0).getOri_price()) / 100);
            txt_old_goodsprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        } else {
            txt_old_goodsprice.setText("￥0");
            txt_old_goodsprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        }
        txt_tuwenxiangqing.setText(detailBean.getSynopsis().trim());
        txt_kuaidi.setText("邮费：包邮");
        txt_chandi.setText(detailBean.getPlace_origin());

        //轮播
        if (detailBean != null) {
            List<String> imgIds = detailBean.getCarousel_images();
            imgIds.add(0, detailBean.getMain_img());
            ArrayList<ImageView> arrayBmps = new ArrayList<ImageView>();
            for (int i = 0; i < imgIds.size(); i++) {
                SimpleDraweeView imageView = new SimpleDraweeView(GoodDetailActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                String m = imgIds.get(i);
                ImageLoader.loadImage(imageView, Const.IMAGE_HEAD + m);
                arrayBmps.add(imageView);
                imageView.setTag(i);
            }
            topLunbo.setImageBitmaps(arrayBmps);
            // //让顶部控件获取到焦点这样滚动条就自然停在顶部了
            topLunbo.setFocusable(true);
            topLunbo.setFocusableInTouchMode(true);
            topLunbo.requestFocus();
        }
        //属性
//        if (detailBean.getProperties() != null) {
//            if (detailBean.getProperties().size() > 0) {
//                for (int i = 0; i < detailBean.getProperties().size(); i++) {
//                    View viewStore = LayoutInflater.from(getApplicationContext())
//                            .inflate(R.layout.item_shuxing, null);
//                    TextView txt_goodname = (TextView) viewStore.findViewById(R.id.txt_goodname);
//
//                    StringBuffer sb=new StringBuffer();
//
//                    if (detailBean.getProperties().get(i).getProp_value_list().size() > 0) {
//                        for (int j = 0; j < detailBean.getProperties().get(i).getProp_value_list().size(); j++) {
//
//                            sb.append(detailBean.getProperties().get(i).getProp_value_list().get(j).getValue_name()+";");
//                        }
//                    }
//                    String value_name=sb.substring(0,sb.length()-1);
//
//                    txt_goodname.setText(detailBean.getProperties().get(i).getProp_name() + "  :  "+value_name);
//                    LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                    layoutparams.topMargin = 1;
//                    shuxing.addView(viewStore, layoutparams);
//
//                }
//            }
//        }

    }


}